package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HyrjAppointment;
import com.ruoyi.system.domain.HyrjAppointmentAction;
import com.ruoyi.system.domain.HyrjUseMachine;
import com.ruoyi.system.mapper.HyrjAppointmentMapper;
import com.ruoyi.system.service.IHyrjAppointmentActionService;
import com.ruoyi.system.service.IHyrjAppointmentService;
import com.ruoyi.system.service.IHyrjUseMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HyrjMachineMapper;
import com.ruoyi.system.domain.HyrjMachine;
import com.ruoyi.system.service.IHyrjMachineService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-07
 */
@Service
public class HyrjMachineServiceImpl implements IHyrjMachineService {
    @Autowired
    private HyrjMachineMapper hyrjMachineMapper;
    @Autowired
    private IHyrjAppointmentActionService hyrjAppointmentActionService;
    @Autowired
    private HyrjAppointmentMapper hyrjAppointmentMapper;
    @Autowired
    private IHyrjUseMachineService hyrjUseMachineService;


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public HyrjMachine selectHyrjMachineById(Long id) {
        return hyrjMachineMapper.selectHyrjMachineById(id);
    }

    @Override
    public HyrjMachine selectHyrjMachineByNo(String machineNo) {
        return hyrjMachineMapper.selectHyrjMachineByNo(machineNo);
    }


    /**
     * 查询【请填写功能名称】列表
     *
     * @param hyrjMachine 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<HyrjMachine> selectHyrjMachineList(HyrjMachine hyrjMachine) {
        return hyrjMachineMapper.selectHyrjMachineList(hyrjMachine);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param hyrjMachine 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertHyrjMachine(HyrjMachine hyrjMachine) {

        //不能添加重复编码的洗衣机
        HyrjMachine hyrjMachine1 = hyrjMachineMapper.selectHyrjMachineByNo(hyrjMachine.getMachineNo());
        if (null != hyrjMachine1) {
            return 0;
        }

        hyrjMachine.setMachineStatus("1");
        hyrjMachine.setCreateTime(DateUtils.getNowDate());
        return hyrjMachineMapper.insertHyrjMachine(hyrjMachine);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param hyrjMachine 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateHyrjMachine(HyrjMachine hyrjMachine) {
        hyrjMachine.setUpdateTime(DateUtils.getNowDate());
        return hyrjMachineMapper.updateHyrjMachine(hyrjMachine);
    }

    @Override
    public int updateHyrjAppointmentAction(HyrjMachine hyrjMachine) {

        HyrjAppointmentAction hyrjAppointmentAction = new HyrjAppointmentAction();
        hyrjAppointmentAction.setMachineNo(hyrjMachine.getMachineNo());
        hyrjAppointmentAction.setUserId(hyrjMachine.getUserId());
        hyrjAppointmentAction.setAppointmentStatus("1");
        HyrjAppointmentAction hyrjAppointmentAction1 = hyrjAppointmentActionService.selectHyrjAppointmentActionByMachineNo(hyrjAppointmentAction);

        if (null != hyrjAppointmentAction1) {
            hyrjAppointmentAction1.setLaundryStartTime(DateUtils.getNowDate());
            hyrjAppointmentAction1.setLaundryPattern(hyrjMachine.getLaundryPattern());
            hyrjAppointmentAction1.setAppointmentStatus("3");
            hyrjAppointmentActionService.updateHyrjAppointmentAction(hyrjAppointmentAction1);
        }
        HyrjAppointment hyrjAppointment = new HyrjAppointment();
        hyrjAppointment.setMachineNo(hyrjMachine.getMachineNo());
        hyrjAppointment.setUserId(hyrjMachine.getUserId());
        hyrjAppointment.setAppointmentStatus("1");
        HyrjAppointment hyrjAppointment1 = hyrjAppointmentMapper.selectHyrjAppointmentByUserIdAndNo(hyrjAppointment);
        if (null != hyrjAppointment1) {
            hyrjAppointment1.setAppointmentStatus("3");
            hyrjAppointmentMapper.updateHyrjAppointment(hyrjAppointment1);
        }

        HyrjUseMachine hyrjUseMachine = new HyrjUseMachine();
        hyrjUseMachine.setUserId(hyrjMachine.getUserId());
        hyrjUseMachine.setMachineNo(hyrjMachine.getMachineNo());
        hyrjUseMachine.setUseTime(new Date());
        hyrjUseMachineService.insertHyrjUseMachine(hyrjUseMachine);


        hyrjMachine.setMachineStatus("2");
        hyrjMachine.setUpdateTime(DateUtils.getNowDate());
        return hyrjMachineMapper.updateHyrjMachine(hyrjMachine);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHyrjMachineByIds(Long[] ids) {
        return hyrjMachineMapper.deleteHyrjMachineByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHyrjMachineById(Long id) {
        return hyrjMachineMapper.deleteHyrjMachineById(id);
    }
}
